﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

using ImageRecognition2.Forms;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Action Edge Filter
    /// </summary>
    public class ActionEdgeFilter : AbstractCommand
    {
        /// <summary>
        /// 
        /// </summary>
        protected BaseImageFilter m_Filter = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionEdgeFilter"/> class.
        /// </summary>
        /// <param name="_filter">The _filter.</param>
        public ActionEdgeFilter(BaseImageFilter _filter)
        {
            m_Filter = _filter;
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>

        /// <summary>
        /// Gets or sets the filter.
        /// </summary>
        /// <value>The filter.</value>
        public BaseImageFilter Filter
        {
            get { return m_Filter; }
            set { m_Filter = value; }
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            //--------------------------------------------------------
            FormFilter formFilter = new FormFilter((Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone(), 0, 255);
            formFilter.trackbAmount.Scroll += delegate(object senderTemp, EventArgs eTemp)
            {
                Bitmap formFilterpictureBox1BackgroundImageClone = (Bitmap)formFilter.imgOriginal.Clone();
                BaseImageFilter newThreshold = (BaseImageFilter)Activator.CreateInstance(m_Filter.GetType(), new object[] { (byte)formFilter.trackbAmount.Value });
                newThreshold.ProcessingMode = PROCESSING_MODE.Color;
                newThreshold.Execute(formFilterpictureBox1BackgroundImageClone).Dispose();
                formFilter.pictureBox1.BackgroundImage = formFilterpictureBox1BackgroundImageClone;
            };

            if (formFilter.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;

                if (m_Filter.GetType() == typeof(EdgeDetectHomogenity))
                {
                    m_Filter = new EdgeDetectHomogenity((byte)formFilter.trackbAmount.Value);
                }
                else if (m_Filter.GetType() == typeof(EdgeDetectConvolution))
                {
                    EdgeDetectConvolution edgeDetectConvolution = m_Filter as EdgeDetectConvolution;
                    m_Filter = new EdgeDetectConvolution((short)EdgeDetectMode.EDGE_DETECT_KIRSH, (byte)formFilter.trackbAmount.Value);
                }
                else if (m_Filter.GetType() == typeof(EdgeDetectQuick))
                {
                    m_Filter = new EdgeDetectQuick((byte)formFilter.trackbAmount.Value);
                }
                else if (m_Filter.GetType() == typeof(EdgeDetectDifference))
                {
                    m_Filter = new EdgeDetectDifference((byte)formFilter.trackbAmount.Value);
                }
                else if (m_Filter.GetType() == typeof(EdgeEnhance))
                {
                    m_Filter = new EdgeEnhance((byte)formFilter.trackbAmount.Value);
                }

                UnsafeBitmap unsafeBitmap = m_Filter.Execute(bitmap);
                unsafeBitmap.Dispose();
            }
        }
    }
}
